package org.sunspotworld;

import com.sun.spot.io.j2me.radiogram.RadiogramConnection;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;

/**
 *
 * @author Dani
 * @version 1.0
 */

public class MessageReceiveThread extends Thread {

    private Host host;
    private RadiogramConnection radiogramConnection;

    public MessageReceiveThread(Host host) {
        this.host = host;
        try {
            radiogramConnection = (RadiogramConnection)
                    Connector.open("radiogram://:37");
        } catch (IOException ex) {}
    }

    /**
     * Command to receive messages and pass them on to the Host.
     */
    public void run() {
        while (true) {
            try {
                Datagram datagram =
                        radiogramConnection.newDatagram(radiogramConnection.getMaximumLength());
                radiogramConnection.receive(datagram);
                host.receiveMessage(datagram);
            } catch (IOException ioe) {}
        }
    }
}
